package com.atguigu.gmall.order.listerner;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.enums.ProcessStatus;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.atguigu.gmall.rabbit.constants.MQConst;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @Author: lx
 * @Time: 11:51
 * @Description:
 */
@Component
public class CancelOrderConsumer {

    @Autowired
    private OrderInfoService orderInfoService;

    @RabbitListener(queues = MQConst.QUEUE_ORDER_CANCEL2)
    public void cancelOrder(Long orderId, Channel channel, Message message) {
        try {
            System.out.println(new Date() + ":" + orderId);
            //取消订单
            orderInfoService.cancelOrderByOrderId(orderId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //更新订单 支付成功状态
    @RabbitListener(bindings = {
            @QueueBinding(
                    exchange = @Exchange(value = MQConst.EXCHANGE_DIRECT_PAYMENT_PAY),
                    value = @Queue(value = MQConst.QUEUE_PAYMENT_PAY, durable = "true"
                            , autoDelete = "false"),
                    key = MQConst.ROUTING_PAYMENT_PAY
            )
    })
    public void updateOrderStatus(Long orderId, Channel channel, Message message) {
        try {
            System.out.println(new Date() + ":" + orderId);
            //更新订单的状态  已经支付的状态
            orderInfoService.updateOrderStatus(orderId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //商品减库结果消息
    @RabbitListener(bindings = {
            @QueueBinding(
                    exchange = @Exchange(value = MQConst.EXCHANGE_DIRECT_WARE_ORDER),
                    value = @Queue(value = MQConst.QUEUE_WARE_ORDER, durable = "true"
                            , autoDelete = "false"),
                    key = MQConst.ROUTING_WARE_ORDER
            )
    })
    public void decrStock(String mapToJson, Channel channel, Message message) {
        try {
            Map map = JSONObject.parseObject(mapToJson, Map.class);

            //map.put("orderId", wareOrderTask.getOrderId());
            //map.put("status", wareOrderTask.getTaskStatus().toString());
            if("DEDUCTED".equals(map.get("status"))){
                //减库存成功
                orderInfoService.updateOrderStatus(Long.parseLong((String)map.get("orderId")),
                        ProcessStatus.WAITING_DELEVER);
            }else{
                //库存超卖
                orderInfoService.updateOrderStatus(Long.parseLong((String)map.get("orderId")),
                        ProcessStatus.STOCK_EXCEPTION);
            }
            //更新订单的状态  已经支付的状态
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
